// ==UserScript==
// @name           AutoLink to UNSW Library Proxy
// @description    Rewrites links to download conference papers through UNSW library proxy.
// @include        http*://ieeexplore.ieee.org/*
// @include        http*://springerlink.metapress.com/*
// @include        http*://www.springerlink.com/*
// @include        http*://portal.acm.org/*
// @include        http*://dl.acm.org/*
// @include        http*://www.sciencedirect.com/*
// ==/UserScript==

DEBUG = true;

SITES = 
[
	/*
	 *
	 * Basic Idea: 
	             	Rewrite the URL's that leads to "Purchase PDF" on sites like IEEExplore, 
			SpringerLink, ACM such that they point to URL's through UNSW Library proxy 
			which lets you download these PDF's for free as long as you are a student
			with them.
				  
	 * Why:
			Reduce the number of clicks, mouse-keyboard actions before you get hold of
			the desired PDF and start reading.
				  
			They are free for the UNSW students anyway. You shouldn't have to perform 
			10 clicks and keyboard actions before you get them.
				  
			With this script installed in GreaseMonkey, you can just ...
				- search in Google for your conference paper
				- click on the ACM/Springer/IEEE link
				- Once you see the monkey icon (yes, that's right) your PDF is ready to
				  download.
	
	 * Adding Your Own Site:
	 
	{
		url_snip:	"if this variable matches with current URL, actions to re-write link(s) begin",
		tag_name:	"name of the tag containing the target href",
		href_pattern:	"this variable is searched over all anchors (<a href>) for which the link need to be re-written upon a match",
		snr_pattern: 	[
					{ 
						search:		"pattern-to-search-for",
						replace:	"pattern-to-replace-with",
					},
					{ 
						search:		"more-pattern-to-search-for",
						replace:	"more-pattern-to-replace-with",
					},
				]
	},
	*
	*
	*/

	{
		url_snip:	"sciencedirect.com",
		tag_name:	"a",		
		href_pattern:	"sdarticle.pdf",
		snr_pattern: 	[
					{ 
						search:		"www.sciencedirect.com",
						replace:	"www.sciencedirect.com.wwwproxy0.library.unsw.edu.au",
					},
				]
	},	
	{
		url_snip:	"ieeexplore",
		tag_name:	"a",
		href_pattern:	"stamp.jsp",
		snr_pattern:	[
					{
						search:		"ieeexplore.ieee.org",
						replace:	"ieeexplore.ieee.org.wwwproxy0.library.unsw.edu.au",
					},
				]
	},
	{
		url_snip:	"ieeexplore",
		tag_name:	"a",		
		href_pattern:	"Sarticle.jsp",
		snr_pattern: 	[
					{ 
						search:		"ieeexplore.ieee.org",
						replace:	"ieeexplore.ieee.org.wwwproxy0.library.unsw.edu.au",
					},
					{ 
						search:		"articleSale/Sarticle.jsp",
						replace:	"stamp/stamp.jsp",
					},
				]
	},
	{
		url_snip:	"ieeexplore",
		tag_name:	"a",		
		href_pattern:	"articleDetails.jsp",
		snr_pattern: 	[
					{ 
						search:		"ieeexplore.ieee.org",
						replace:	"ieeexplore.ieee.org.wwwproxy0.library.unsw.edu.au",
					},
					{ 
						search:		"xpl//articleDetails.jsp",
						replace:	"stamp/stamp.jsp",
					},
				]
	},	
	{
		url_snip:	"springerlink.*metapress",
		tag_name:	"a",		
		href_pattern:	"fulltext",
		snr_pattern: 	[
					{ 
						search:		"springerlink.metapress.com",
						replace:	"springerlink.metapress.com.wwwproxy0.library.unsw.edu.au",
					},
				]
	},
	{
		url_snip:	"www.*springerlink",
		tag_name:	"a",		
		href_pattern:	"fulltext",
		snr_pattern: 	[
					{ 
						search:		"www.springerlink.com",
						replace:	"www.springerlink.com.wwwproxy0.library.unsw.edu.au",
					},
				]
	},
	{
		url_snip:	"(dl|portal).acm.org",
		tag_name:	"a",		
		href_pattern:	"purchase.cfm",
		snr_pattern: 	[
					{
						search:     "https",
						replace:    "http",
					},
					{ 
						search:		"(dl|portal).acm.org",
						replace:	"dl.acm.org.wwwproxy0.library.unsw.edu.au",
					},
					{ 
						search:		"purchase.cfm\\?",
						replace:	"ft_gateway.cfm?type=pdf&coll=DL&",
					},
				]
	},
	{
		url_snip:	"sciencedirect.com",
		tag_name:	"a",		
		href_pattern:	"science/article/pii",
		snr_pattern: 	[
					{ 
						search:		"www.sciencedirect.com",
						replace:	"www.sciencedirect.com.wwwproxy0.library.unsw.edu.au",
					},
					{ 
						search:		"#",
						replace:	"",
					},                    
				]
	},
]


monkey_icon = ""


function find_tags(tag_name, href_pattern)
{
	var tags = document.getElementsByTagName(tag_name);
	var href_tags = new Array();
	
	for (var i=0;i<tags.length;i++)
	{
		if (tags[i].href.match(href_pattern))
		{
			href_tags[href_tags.length] = tags[i];
		}
	}

	return href_tags;
}


function rewrite_href(href_tags, snr_pattern)
{
	for (var i=0;i<href_tags.length;i++)
	{		
		/* replace old link, with new */
		newElement = document.createElement("a");
		node = href_tags[i].parentNode.insertBefore(newElement, href_tags[i].nextSibling);
		node.style.textDecoration = "blink";
		node.innerHTML = "<bold><img style='border:0px' src=" + monkey_icon + "></bold>";
		node.href = href_tags[i].href;
		node.target = "_blank";
		href_tags[i].parentNode.removeChild(href_tags[i]);
		
		/* re-write href of the new link */
		for (var j=0;j<snr_pattern.length;j++)
		{
			node.href = node.href.replace(new RegExp(snr_pattern[j].search),snr_pattern[j].replace);
            node.target = "_self";
		}

        /* Babil: ugly */
        if (i > 0) break;
	}
}


for (var i=0;i<SITES.length;i++)
{	
	if (document.URL.match(SITES[i].url_snip)) 
	{
        if (DEBUG) console.log("URL matched " + SITES[i].url_snip);
		var href_tags = find_tags(SITES[i].tag_name, SITES[i].href_pattern);
		if ( href_tags.length > 0 )
		{
			if (DEBUG) console.log("Pattern matched " + SITES[i].snr_pattern);
            rewrite_href(href_tags, SITES[i].snr_pattern);
		}
	}
}

